﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightCoverage.RepRef;
using System.Windows.Browser;

namespace SilverlightCoverage
{
    public partial class CodeDisplay : UserControl
    {
        string moduleName = string.Empty;
        string namespaceName = string.Empty;
        string classname = string.Empty;
        string key = string.Empty;
        string value = string.Empty;
        string rptFile = string.Empty;
        System.Collections.ObjectModel.ObservableCollection<MethodInfo> methodInfoColln;
        public CodeDisplay()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CodeDisplay_Loaded);
        }

        void CodeDisplay_Loaded(object sender, RoutedEventArgs e)
        {

            IDictionary<string, string> qString = HtmlPage.Document.QueryString;
            foreach (KeyValuePair<string, string> kvp in qString)
            {
                key = kvp.Key;
                value = kvp.Value;
                if (key == "mod1" || key == "modname")
                    moduleName = value;
                if (key == "nName")
                    namespaceName = value;
                if (key == "rptLink")
                    rptFile = value;
                if (key == "clsName")
                    classname = value;
            }

            PopulateClassMethodCoverage(moduleName, namespaceName, classname, rptFile);
            IsimpleWcfClient proxy = new IsimpleWcfClient();
            proxy.GetFileOnfoCompleted += new EventHandler<GetFileOnfoCompletedEventArgs>(proxy_GetFileOnfoCompleted);

            //proxy.GetFileOnfoAsync("Microsoft.Practices.Composite.dll", "Microsoft.Practices.Composite.Modularity", "ModuleTypeLoadingException", @"C:\karthik\CAL.xml");
            proxy.GetFileOnfoAsync(moduleName, namespaceName, classname, rptFile);
        }

        void proxy_GetFileOnfoCompleted(object sender, GetFileOnfoCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                System.Collections.ObjectModel.ObservableCollection<FileContainer> filecollection = e.Result;

                foreach (FileContainer fc in filecollection)
                {
                    ListBox lb = new ListBox();
                    lb.ItemsSource = fc.SrcCodeInfoCollection;
                    lb.ItemTemplate = dtemp;
                    string a = fc.FileName.Substring(fc.FileName.LastIndexOf('\\') + 1);

                    srcContainer.Items.Add(new TabItem { Header = a, Content = lb });
                }
            }

        }

        void PopulateClassMethodCoverage(string modName, string nsName, string clsName, string strXMLPath)
        {
            try
            {
                IsimpleWcfClient aggrProxy = new IsimpleWcfClient();

                aggrProxy.GetMethodInfoCompleted += new EventHandler<GetMethodInfoCompletedEventArgs>(aggrProxy_GetMethodInfoCompleted);
                aggrProxy.GetMethodInfoAsync(modName, nsName, clsName, strXMLPath);
                //MtdInfoCollection = aggrProxy.GetMethodInfo(modName, nsName, clsName, strXMLPath);
                //DataTable dtTable = CreateDataTable(MtdInfoCollection);
                //Session["MtdDataTable"] = dtTable;
                //DataView dv = new DataView(dtTable);
                //dv.Sort = "CovPercentage ASC";
                //gvClsMtdCovAggregate.DataSource = dv;
                //gvClsMtdCovAggregate.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        void aggrProxy_GetMethodInfoCompleted(object sender, GetMethodInfoCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                methodInfoColln = e.Result;
                if (methodInfoColln.Count >= 8)
                {
                    dgBorder.Height = 180;
                }
                else if (methodInfoColln.Count < 8)
                {
                    dgBorder.Height = methodInfoColln.Count * 18.5 + 30;
                }
                gridMethodNames.ItemsSource = methodInfoColln.OrderBy(x => x.CovPercentage); 
            }
            
        }

        private void hyperMethods_Click(object sender, RoutedEventArgs e)
        {
            MethodInfo mInfo = (MethodInfo)gridMethodNames.SelectedItem;            
            foreach (TabItem ti in srcContainer.Items)
            {
                if (ti.Header.ToString() == mInfo.FileName)
                {
                    srcContainer.SelectedItem = ti;
                    break;
                }

            }

            HightLightCode(mInfo.LineNumber, srcContainer.SelectedIndex);

        }
        void HightLightCode(int lineNumber, int tagID)
        {

            TabItem titem = (TabItem)srcContainer.SelectedItem;
            ListBox content = (ListBox)titem.Content;
            content.SelectedIndex = lineNumber - 1;
            ListBoxItem item = content.Items[content.SelectedIndex] as ListBoxItem;
            content.ScrollIntoView(content.Items[content.SelectedIndex]);
        }
    }
               
}
